package com.sfac.hqyj.springBoot.modules.test.controller;

import com.github.pagehelper.PageInfo;
import com.sfac.hqyj.springBoot.modules.common.entity.ResultEntity;
import com.sfac.hqyj.springBoot.modules.common.entity.SearchBean;
import com.sfac.hqyj.springBoot.modules.test.entity.City;
import com.sfac.hqyj.springBoot.modules.test.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/api")
@ResponseBody
public class CityController {
    @Autowired
    private CityService cityService;


    /**
     * 127.0.0.1/api/city ---- post
     * {"cityName":"hujiang","localCityName":"胡江","countryId":522}
     */
    @PostMapping(value = "/city", consumes = "application/json")
    public ResultEntity<City> insertCity(@RequestBody City city) {
        return cityService.insertCity(city);
    }

    /**
     * 127.0.0.1/api/city ---- put
     * cityId=2261,cityName=dreamCity,localCityName=梦想城市1,
     * countryId=522,district=sichuan,population=1780001
     */
    @PutMapping(value = "/city", consumes = "application/x-www-form-urlencoded")
    public ResultEntity<City> updateCity(@ModelAttribute City city) {
        return cityService.updateCity(city);
    }

    /**
     * 127.0.0.1/api/city/2258 ---- delete
     */
    @DeleteMapping("/city/{cityId}")
    public ResultEntity<Object> deleteCity(@PathVariable int cityId) {
        return cityService.deleteCity(cityId);
    }

    /**
     * 127.0.0.1/api/city/1890 ---- get
     */
    @GetMapping("/city/{cityId}")
    public City getCityByCityId(@PathVariable int cityId) {
        return cityService.getCityByCityId(cityId);
    }

    /**
     * 127.0.0.1/api/cities/522 ---- get
     */
    @GetMapping("/cities/{countryId}")
    public List<City> getCitiesByCountryId(@PathVariable int countryId) {
        return cityService.getCitiesByCountryId(countryId);
    }

    /**
     * 127.0.0.1/api/cities ---- post
     * {"currentPage":1, "pageSize":5, "keyWord":"shanghai", "orderBy":"city_id",
     * "direction":"asc"}
     */
    @PostMapping(value = "/cities", consumes = "application/json")
    public PageInfo<City> getCitiesBySearchBean(@RequestBody SearchBean searchBean) {
        return cityService.getCitiesBySearchBean(searchBean);
    }
}
